package com.aynu.webshop.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aynu.webshop.web.model.Admin;
import com.aynu.webshop.web.model.User;
import com.aynu.webshop.web.service.AdminService;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {
	
	@Resource
	private AdminService adminService;
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：管理员跳转到登录页面 
	 */
	@RequestMapping(value = "/adminloginJsp", method = RequestMethod.GET)
	public ModelAndView loginJsp(HttpServletRequest request ,User user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("../../adminlogin");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：管理员登录 
	 */
	@RequestMapping(value = "/adminlogin", method = RequestMethod.POST)
	public ModelAndView adminlogin(HttpServletRequest request, Admin admin) {
		ModelAndView mv = new ModelAndView();
		Admin confirm = adminService.findByAname(admin.getaName());
		if (confirm != null) {
			if (confirm.getaPassword().equals(admin.getaPassword())) {
				request.getSession().setAttribute("admin", confirm);
					mv.setViewName("../../admin");
			} else {
				request.setAttribute("failedInfo", "登录失败,密码错误！");
				mv.setViewName("../../login");
			}
		} else {
			request.setAttribute("failedInfo", "登录失败,没有该用户！");
			mv.setViewName("../../login");
		}
		return mv;
	}
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：管理员登出 
	 */
	@RequestMapping(value = "/adminlogout", method = RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		request.getSession().removeAttribute("admin");
		mv.setViewName("../../adminlogin");
		return mv;
	}

}
